let RoomState = require("./RoomState");
let Player = require("./Player.class");
let RoomController = require("../controller/RoomController.class");
let EventListener = require("../core/EventListener.class");
let MessageId = require("./MessageId");
class Room{
    constructor(data){
        let self = this;
        this.className = "roomId_";
        this.players = [];
        this.curentRound = 0;
        this.options = data.options || [];
        this.roomId = data["roomId"];
        this.roomState = RoomState.Created;
        this.totalRound = data.totalRound || 10000;
        this.maxPlayer = data.maxPlayer || 6;
        this.mapId = data.mapId;
        this.seatMap = {};
        this.className += this.roomId;
        this.turnSeat = 0;

        // 事件监听
        EventListener.getInstance().addListener(this.className, MessageId.Prepare, function (data) {
            let player = self.getPlayerByUid(data["uid"]);
            if(player) {
                player.setPrepare(data["prepare"]);
            }
            let messageObj = {};
            messageObj.code = "prepare";
            messageObj.data = data;
            RoomController.getInstance().broadcast(self.players, messageObj);
            /*
            * 1、告诉房主是否可以开始游戏
            * 2、所有玩家准备，直接开始
            * 3、服务器发牌
            * */
            if (self.players.length > 1 && self.isStart()){
                self.clearData(); // 清除上局数据
                self.shuffle(self.players);// 洗牌
                self.dealCards();// 发牌
                self.onTurn();   // 操作
            }
        });
        EventListener.getInstance().addListener(this.className, MessageId.Exit, function (data) {
            self.removePlayer(data);
            // 销毁房间
            if (self.players.length === 0){
                let messageObj = {};
                messageObj.code = MessageId.DestroyRoom;
                messageObj.data = {};
                messageObj.data.roomId = self.roomId;
                EventListener.getInstance().postMessage(messageObj);
                EventListener.getInstance().removeListener(self.className);
            }
        });
    }
    addPlayer(info){
        let player = new Player(info);
        player.setSeat(this.players.length);
        this.players.push(player);

        // 1、广播房间中有多少玩家
        let messageObj = {};
        messageObj.code = MessageId.JoinRoom;
        messageObj.data = {};
        messageObj.data.roomId = this.roomId;
        messageObj.data.mapId = this.mapId;
        messageObj.data.roomState = this.roomState;
        messageObj.data.curentRound = this.curentRound;
        messageObj.data.totalRound = this.totalRound;
        messageObj.data.players = [];
        for (let i = 0; i < this.players.length; i++){
            messageObj.data.players.push(this.players[i].getConfig());
        }
        RoomController.getInstance().broadcast(this.players, messageObj);

        // 2、广播准备，其他玩家都需要知道这个玩家的准备状态
        // let messageObj = {};
        // messageObj.code = "prepare";
        // messageObj.data = {};
        // messageObj.data.uid = player.getUid();
        // messageObj.data.state = player.isPrepare();
        // RoomController.getInstance().broadcast(this.players, messageObj);
        console.log("房间中有【" + this.players.length + "】个用户");
    }
    removePlayer(data){
        let uid = data.uid;
        for (let i = 0; i < this.players.length; i++){
            if (this.players[i].getUid() === uid){
                let messageObj = {};
                messageObj.code = MessageId.Exit;
                messageObj.data = {};
                messageObj.data.uid = data.uid;
                RoomController.getInstance().broadcast(this.players, messageObj);
                this.players.splice(i, 1);
                break;
            }
        }
        console.log("房间中有【" + this.players.length + "】个玩家");
    }
    getRoomId(){
        return this.roomId;
    }
    getUserCount(){
        return this.players.length;
    }
    getMaxPlayer(){
        return this.maxPlayer;
    }
    setRoomState(state){
        this.roomState = state;
        switch(this.roomState){
            case RoomState.Created:
                break;
            case RoomState.Prepare:// 准备阶段
                break;
            case RoomState.OnGoing:// 正在游戏
                break;
            case RoomState.GameOver:// 游戏结束
                break;
            case RoomState.End:// 房间解散
                break;
            default:
                break;
        }
    }
    getConfig(){
        let config = {};
        config["roomId"] = this.roomId;
        config["maxPlayer"] = this.maxPlayer;
        config["mapId"] = this.mapId;
        config["roomState"] = this.roomState;
        config["curentRound"] = this.curentRound;
        config["totalRound"] = this.totalRound;
        config["options"] = this.options;
        return config;
    }
    getPlayerByUid(uid){
        let player = null;
        for(let i = 0; i < this.players.length; i++){
            if(this.players[i].getUid() === uid){
                return player = this.players[i];
            }
        }
        console.log("房间【"+this.roomId+"】没有玩家【"+uid+"】");
        return player;
    }
    getPlayerBySeat(seat){
        let player = null;
        for(let i = 0; i < this.players.length; i++){
            if(this.players[i].getSeat() === seat){
                return player = this.players[i];
            }
        }
        console.log("房间【"+this.roomId+"】没有座位号【"+seat+"】");
        return player;
    }
    getPlayers(){
        return this.players;
    }
    isStart(){
        for (let i = 0; i < this.players.length; i++) {
            if (!this.players[i].isPrepare()){
                return false;
            }
        }
        return true;
    }
    getUidBySeat(seat){
        for (let i = 0; i < this.players.length; i++){
            if (this.players[i].seat === seat){
                return this.players[i].getUid();
            }
        }
        console.log("找不到Seat【"+seat+"】"+"的玩家");
        return -1;
    }
    // 下一轮
    nextRound(){
        console.log("下一局开始...");
        this.setRoomState(RoomState.Prepare);
        this.curentRound++;
        this.turnSeat = 0;

        // 重置准备状态
        // 广播准备
        let data = {};
        data.prepares = [];
        for(let i = 0; i < this.players.length; i++){
            let player = this.players[i];
            player.setPrepare(false);
            data.prepares.push({uid:player.getUid(),prepare:player.isPrepare()});
        }
        let messageObj = {};
        messageObj.code = MessageId.PrepareBroadcast;
        messageObj.data = data;
        RoomController.getInstance().broadcast(this.players, messageObj);
    }

}
module.exports = Room;